package com.example.xf.test;

import xf.tools.PullToRefreshView;
import xf.tools.PullToRefreshView.OnFooterRefreshListener;
import xf.tools.PullToRefreshView.OnHeaderRefreshListener;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ArrayAdapter;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.vgo.app.swipemenulistview.SwipeMenu;
import com.vgo.app.swipemenulistview.SwipeMenuCreator;
import com.vgo.app.swipemenulistview.SwipeMenuItem;
import com.vgo.app.swipemenulistview.SwipeMenuListView;
import com.vgo.app.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;

/** 下拉上拉侧滑菜单刷新界面 */
public class ListActivity extends BaseActivity implements OnHeaderRefreshListener, OnFooterRefreshListener {

	private static final String TAG = "ListActivity";

	SwipeMenuListView swip_list;

	private PullToRefreshView mPullToRefreshView;

	@Override
	protected void onResume() {
		BaseActivity.activity = this; // TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.listview_activity);
		BaseActivity.activity = this;
		swip_list = (SwipeMenuListView) findViewById(R.id.swip_list);
		mPullToRefreshView = (PullToRefreshView) findViewById(R.id.main_pull_refresh_view);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);

		initMenuListView();

		String[] strs = { "第一", "第二", "第三", "第四", "第一", "第二", "第三", "第四", "第一", "第二", "第三", "第四" };

		swip_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs));

	}

	private void initMenuListView() {
		// 创建一个SwipeMenuCreator供ListView使用
		SwipeMenuCreator creator = new SwipeMenuCreator() {
			@Override
			public void create(SwipeMenu menu) {
				// 创建一个侧滑菜单
				SwipeMenuItem Item0 = new SwipeMenuItem(getApplicationContext());
				// 给该侧滑菜单设置背景
				Item0.setBackground(new ColorDrawable(Color.rgb(0xB2, 0x3D, 0x6B)));
				// 设置宽度
				Item0.setWidth(dp2px(60));
				// 设置图片
				Item0.setIcon(R.drawable.icon_confirm_unfocused);
				// 加入到侧滑菜单中
				menu.addMenuItem(Item0);

				SwipeMenuItem Item1 = new SwipeMenuItem(getApplicationContext());
				// 给该侧滑菜单设置背景
				Item1.setBackground(new ColorDrawable(Color.rgb(0xB2, 0x3D, 0x6B)));
				// 设置宽度
				Item1.setWidth(dp2px(60));
				// 设置图片
				Item1.setIcon(R.drawable.icon_del_unfocused);
				// 加入到侧滑菜单中
				menu.addMenuItem(Item1);

				SwipeMenuItem Item2 = new SwipeMenuItem(getApplicationContext());
				// 给该侧滑菜单设置背景
				Item2.setBackground(new ColorDrawable(Color.rgb(0xB2, 0x3D, 0x6B)));
				// 设置宽度
				Item2.setWidth(dp2px(60));
				// 设置图片
				Item2.setIcon(R.drawable.ic_launcher);
				// 加入到侧滑菜单中
				menu.addMenuItem(Item2);
			}
		};
		swip_list.setMenuCreator(creator);
		// 侧滑菜单的相应事件
		swip_list.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
				switch (index) {
				case 0:// 第一个添加的菜单的响应事件
					Log.w("侧滑菜单的相应事件1", "被点击");
					startActivity(new Intent(ListActivity.this, PopuActivity.class));
					EmmApplication.startIntentAnim2(ListActivity.this);
					break;
				case 1:// 第二个添加的菜单的响应事件
					Log.w("侧滑菜单的相应事件2", "被点击");
					break;
				case 2:// 第三个添加的菜单的响应事件
					Log.w("侧滑菜单的相应事件3", "被点击");
					break;
				default:
					break;
				}
				return false;
			}
		});
	}

	// 将dp转化为px
	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		mPullToRefreshView.onHeaderRefreshComplete();// 刷新动画消失
		Log.w(TAG, "onHeaderRefresh");
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		mPullToRefreshView.onFooterRefreshComplete();// 加载动画消失
		Log.w(TAG, "onFooterRefresh");
	}
}
